package com.th.supcom.pds.config.client.widget;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.extjs.gxt.ui.client.widget.grid.GridViewConfig;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.user.client.Element;
import com.th.supcom.pds.config.client.common.PdsConfigConstant;
import com.th.supcom.pds.config.client.common.PdsConfigEnableEnum;
import com.th.supcom.pds.config.client.model.PhamWindowConfigModel;

/**
 * PhamWindowConfig主面板.
 * 
 * 药房窗口属性列表.
 */
public class PhamWindowConfigMainPanel extends ContentPanel
{
    /** 公司版权信息. */
    public static final String copyright = "Copyright 2014, TH Supcom Corporation";
    /** 提交路径. */
    public static final String SCC_BRANCH = "$URL$";
    /** 提交日期. */
    public static final String SCC_LAST_MODIFICATION_DATE = "$Date$";
    /** 最终修改人员. */
    public static final String SCC_LAST_MODIFIER_NAME = "$Author$";

    /** 最终版本号. */
    public static final String SCC_REVISION = "$Revision$";

    PhamWindowConfigAction m_action = new PhamWindowConfigAction ();

    /** 操作按钮 */
    private Button m_addButton;

    private Button m_editButton;

    private final ListStore <PhamWindowConfigModel> m_listStore = new ListStore <PhamWindowConfigModel> ();

    private Button m_refreshButton;

    private Button m_removeButton;

    /** Grid数据 */
    private Grid <PhamWindowConfigModel> m_windowGrid;

    public PhamWindowConfigMainPanel ()
    {
        this.initUI ();
        m_action.registerListeners (this);
    }

    /**
     * 生成窗口属性表格列字段
     * 
     * @return
     */
    private ColumnModel createColumnModel ()
    {
        final List <ColumnConfig> configs = new ArrayList <ColumnConfig> ();

        ColumnConfig column = new ColumnConfig ();
        PhamConfigWidgetUtils.generateGridSeq (configs, column);

        column = new ColumnConfig ("deptName", "所属药房", 100);
        column.setAlignment (HorizontalAlignment.LEFT);
        configs.add (column);

        column = new ColumnConfig ("windowName", "窗口名称", 100);
        column.setAlignment (HorizontalAlignment.CENTER);
        configs.add (column);

        column = new ColumnConfig ("windowIp", "IP地址", 100);
        column.setAlignment (HorizontalAlignment.CENTER);
        configs.add (column);

        column = new ColumnConfig ("serviceName", "服务类型", 60);
        column.setAlignment (HorizontalAlignment.CENTER);
        configs.add (column);

        column = new ColumnConfig ("prescTypeName", "处方类型", 60);
        column.setAlignment (HorizontalAlignment.CENTER);
        configs.add (column);

        column = new ColumnConfig ("openFlag", "是否" + PdsConfigConstant.DEFALUT_HTML_WRAP + "启用", 45);
        column.setAlignment (HorizontalAlignment.CENTER);
        renderEnableBtn (column);
        configs.add (column);

        column = new ColumnConfig ("slipPrinter", "消费凭证" + PdsConfigConstant.DEFALUT_HTML_WRAP + "打印机", 110);
        column.setAlignment (HorizontalAlignment.CENTER);
        configs.add (column);

        column = new ColumnConfig ("dispensePrinter", "摆药单" + PdsConfigConstant.DEFALUT_HTML_WRAP + "打印机", 110);
        column.setAlignment (HorizontalAlignment.CENTER);
        configs.add (column);

        column = new ColumnConfig ("notDispensePrinter", "未摆药单" + PdsConfigConstant.DEFALUT_HTML_WRAP + "打印机", 110);
        column.setAlignment (HorizontalAlignment.CENTER);
        configs.add (column);

        column = new ColumnConfig ("refundApplyPrinter", "退费申请" + PdsConfigConstant.DEFALUT_HTML_WRAP + "打印机", 110);
        column.setAlignment (HorizontalAlignment.CENTER);
        configs.add (column);

        column = new ColumnConfig ("withdrawlPrinter", "退药单" + PdsConfigConstant.DEFALUT_HTML_WRAP + "打印机", 110);
        column.setAlignment (HorizontalAlignment.CENTER);
        configs.add (column);

        final ColumnModel cm = new ColumnModel (configs);
        return cm;
    }

    private void enableBtnAction (final PhamWindowConfigModel configModel, final Integer updateValue)
    {
        final PhamWindowConfigModel updateModel = new PhamWindowConfigModel ();
        updateModel.setPhamWindowConfigId (configModel.getPhamWindowConfigId ());
        updateModel.setDeptCode (configModel.getDeptCode ());
        updateModel.setWindowIp (configModel.getWindowIp ());
        updateModel.setWindowName (configModel.getWindowName ());
        updateModel.setServiceType (configModel.getServiceType ());
        updateModel.setOpenFlag (updateValue);
        m_action.enableBtnListener (updateModel, this);
    }

    public Button getAddButton ()
    {

        return m_addButton;
    }

    public Button getEditButton ()
    {

        return m_editButton;
    }

    public ListStore <PhamWindowConfigModel> getListStore ()
    {

        return m_listStore;
    }

    public Button getRefreshButton ()
    {

        return m_refreshButton;
    }

    public Button getRemoveButton ()
    {

        return m_removeButton;
    }

    public Grid <PhamWindowConfigModel> getWindowGrid ()
    {

        return m_windowGrid;
    }

    private void initDispensaryConfigComponent ()
    {
        m_addButton = new Button ();
        m_editButton = new Button ();
        m_removeButton = new Button ();
        m_refreshButton = new Button ();
        final ToolBar toolBar = PhamConfigWidgetUtils.generateToolBar (m_addButton, m_editButton, m_removeButton,
                                                                       m_refreshButton);

        m_windowGrid = new Grid <PhamWindowConfigModel> (m_listStore, createColumnModel ());
        m_windowGrid.setStateful (true);
        m_windowGrid.setStyleAttribute ("borderTop", "none");
        m_windowGrid.setStripeRows (true);
        m_windowGrid.getView ().setForceFit (true);
        m_windowGrid.getView ().setEmptyText (PdsConfigConstant.GRID_NOT_RECORD);
        m_windowGrid.setBorders (false);
        m_windowGrid.setLoadMask (true);
        m_windowGrid.setColumnLines (true);
        m_windowGrid.getSelectionModel ().setSelectionMode (SelectionMode.SINGLE);
        m_windowGrid.getView ().setViewConfig (new GridViewConfig ()
        {
            @Override
            public String getRowStyle (final ModelData model, final int rowIndex, final ListStore <ModelData> ds)
            {
                final PhamWindowConfigModel configModel = (PhamWindowConfigModel) model;
                if (configModel != null && configModel.getOpenFlag () != null
                    && PdsConfigEnableEnum.Disable.getCode () == configModel.getOpenFlag ())
                {
                    return PdsConfigConstant.GRAY;
                }
                return null;
            }
        });
        this.add (m_windowGrid);
        this.setTopComponent (toolBar);
    }

    /**
     * 初始化UI控件.
     * 
     */
    private void initUI ()
    {
        this.setLayout (new FitLayout ());
        this.setHeaderVisible (false);
        this.setBodyBorder (false);
        this.setBorders (false);
        this.setStyleAttribute ("margin", "1");
        initDispensaryConfigComponent ();
    }

    @Override
    protected void onRender (final Element parent, final int index)
    {
        super.onRender (parent, index);
    }

    /**
     * 添加启用按钮的点击事件
     * 
     * @param column
     */
    private void renderEnableBtn (final ColumnConfig column)
    {
        column.setRenderer (new GridCellRenderer <ModelData> ()
        {

            @Override
            public Object render (final ModelData model, final String property, final ColumnData config,
                                  final int rowIndex, final int colIndex, final ListStore <ModelData> store,
                                  final Grid <ModelData> grid)
            {
                final PhamWindowConfigModel configModel = (PhamWindowConfigModel) model;
                final Button btn = new Button ();
                btn.setWidth (35);
                btn.setAutoHeight (true);
                final Integer updateValue;
                if (configModel.getOpenFlag () == PdsConfigEnableEnum.Enable.getCode ())
                {
                    btn.setText ("禁用");
                    updateValue = PdsConfigEnableEnum.Disable.getCode ();
                }
                else
                {
                    btn.setText ("启用");
                    updateValue = PdsConfigEnableEnum.Enable.getCode ();
                }
                btn.addSelectionListener (new SelectionListener <ButtonEvent> ()
                {

                    @Override
                    public void componentSelected (final ButtonEvent ce)
                    {
                        enableBtnAction (configModel, updateValue);
                    }

                });
                return btn;

            }
        });
    }
}